Imports System.Data.SqlClient

Public Class cls_modulo_

    Implements ICloneable

#Region "Variables ..."
    Private str_idmodulo As String = ""
    Private str_modulo As String = ""
    Private str_grupo As String = ""
    Private str_subgrupo As String = ""
    Private str_formulario As String = ""
    Private bit_esroot As Boolean = False

    Private obj_mnuGrupo As DevExpress.XtraNavBar.NavBarGroup
    Private obj_config_modulo As cls_config_modulo_

    Private col_Items As Collections.Generic.List(Of cls_modulo_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idmodulo
        End Get
    End Property
    Property idmodulo() As String
        Get
            idmodulo = str_idmodulo
        End Get
        Set(ByVal value As String)
            str_idmodulo = value
        End Set
    End Property
    Property modulo() As String
        Get
            modulo = str_modulo
        End Get
        Set(ByVal value As String)
            str_modulo = value
        End Set
    End Property
    Property grupo() As String
        Get
            grupo = str_grupo
        End Get
        Set(ByVal value As String)
            str_grupo = value
        End Set
    End Property
    Property subgrupo() As String
        Get
            subgrupo = str_subgrupo
        End Get
        Set(ByVal value As String)
            str_subgrupo = value
        End Set
    End Property
    Property formulario As String
        Get
            formulario = str_formulario
        End Get
        Set(value As String)
            str_formulario = value
        End Set
    End Property
    Property esroot As Boolean
        Get
            esroot = bit_esroot
        End Get
        Set(value As Boolean)
            bit_esroot = value
        End Set
    End Property
    Property mnuGrupo As DevExpress.XtraNavBar.NavBarGroup
        Get
            mnuGrupo = obj_mnuGrupo
        End Get
        Set(value As DevExpress.XtraNavBar.NavBarGroup)
            obj_mnuGrupo = value
        End Set
    End Property
    Property config_modulo() As cls_config_modulo_
        Get
            If IsNothing(obj_config_modulo) Then
                obj_config_modulo = New cls_config_modulo_
                If Not obj_config_modulo.Buscar(idmodulo) Then
                    obj_config_modulo = Nothing
                End If
            End If
            config_modulo = obj_config_modulo
        End Get
        Set(ByVal value As cls_config_modulo_)
            obj_config_modulo = value
        End Set
    End Property


    Property Items() As Collections.Generic.List(Of cls_modulo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_modulo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_modulo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_modulo_
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo"))) = False Then
                Elemento.str_idmodulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("modulo"))) = False Then
                Elemento.str_modulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("modulo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("grupo"))) = False Then
                Elemento.str_grupo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("grupo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("subgrupo"))) = False Then
                Elemento.str_subgrupo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("subgrupo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("formulario"))) = False Then
                Elemento.str_formulario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("formulario")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("esroot"))) = False Then
                Elemento.bit_esroot = (obj_Reader.GetValue(obj_Reader.GetOrdinal("esroot")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo"))) = False Then
            str_idmodulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("modulo"))) = False Then
            str_modulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("modulo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("grupo"))) = False Then
            str_grupo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("grupo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("subgrupo"))) = False Then
            str_subgrupo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("subgrupo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("formulario"))) = False Then
            str_formulario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("formulario")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("esroot"))) = False Then
            bit_esroot = (obj_Reader.GetValue(obj_Reader.GetOrdinal("esroot")))
        End If
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulo__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de modulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Buscar(ByVal idmodulo As String) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulo__Buscar"
                .Parameters.Add("@idmodulo", SqlDbType.NVarChar).Value = idmodulo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar los registros de modulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarTodosOrdenado() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodosOrdenado = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "modulo__CargarTodosOrdenado"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodosOrdenado = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodosOrdenado los registros de modulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_modulo_Clon As New cls_modulo_
        obj_modulo_Clon.str_idmodulo = Me.str_idmodulo
        obj_modulo_Clon.str_modulo = Me.str_modulo
        obj_modulo_Clon.str_grupo = Me.str_grupo
        obj_modulo_Clon.str_subgrupo = Me.str_subgrupo
        obj_modulo_Clon.col_Items = Me.col_Items
        Return obj_modulo_Clon
    End Function

End Class
